package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.mapper.FlowerpotTypeMapper;
import com.wjh.pojo.FlowerpotCompany;
import com.wjh.pojo.FlowerpotType;
import com.wjh.service.FlowerpotCompanyService;
import com.wjh.service.FlowerpotService;
import com.wjh.service.FlowerpotTypeService;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 花盆种类(FlowerpotType)表服务实现类
 *
 * @author makejava
 * @since 2023-10-17 19:38:50
 */
@Service("flowerpotTypeService")
public class FlowerpotTypeServiceImpl extends ServiceImpl<FlowerpotTypeMapper, FlowerpotType> implements FlowerpotTypeService {

    @Resource
    private FlowerpotCompanyService flowerpotCompanyService;

    @Resource
    private FlowerpotService flowerpotService;

    @Override
    public ResponseResult flowerpotTypeList(Integer pageNum, Integer pageSize, String typeName, Integer type) {
        LambdaQueryWrapper<FlowerpotType> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.like(typeName != null,FlowerpotType::getName,typeName);
        queryWrapper.eq(type != null,FlowerpotType::getType,type);

        Page<FlowerpotType> certificatePage = new Page<>(pageNum,pageSize);
        Page<FlowerpotType> page = page(certificatePage, queryWrapper);
        List<FlowerpotType> records = page.getRecords();
        for (FlowerpotType record : records) {
            //根据typeId对所属花盆公司进行赋值
            FlowerpotCompany flowerpotCompany  = flowerpotCompanyService.getNameById(record.getType());
            record.setCompanyName(flowerpotCompany.getName());
        }

        return ResponseResult.okResult(new PageVo(records,page.getTotal()));
    }

    @Override
    public ResponseResult flowerpotTypeAdd(FlowerpotType flowerpotType) {
        if(flowerpotType.getType() == null){
            throw new SystemException(AppHttpCodeEnum.NEED_TYPE);
        }
        if(!StringUtils.hasText(flowerpotType.getName())){
            throw new SystemException(AppHttpCodeEnum.NEED_NAME);
        }
        save(flowerpotType);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delByTypeIds(List<Integer> id) {
        for (Integer integer : id) {
            //根据id删除对应分类下的花盆
            flowerpotService.delByTypeIds(integer);
        }

        LambdaQueryWrapper<FlowerpotType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(FlowerpotType::getId,id);
        remove(queryWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public void delByCompanyId(List<Integer> id) {
        for (Integer integer : id) {
            LambdaQueryWrapper<FlowerpotType> queryWrapper =
                    new LambdaQueryWrapper<>();
            queryWrapper.eq(FlowerpotType::getType,integer);
            List<FlowerpotType> list = list(queryWrapper);
            for (FlowerpotType flowerpotType : list) {
                flowerpotService.delByTypeIds(flowerpotType.getId());
            }
            remove(queryWrapper);
        }
    }
}
